package ru.isys.view;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import ru.isys.activity.MainActivity;
import ru.isys.xml.XmlItem;

public class ModelPanel extends JPanel implements HaveMainListener {

	List<ModelItem> elements;
	GridBagConstraints constraints;
	GridBagLayout gridbag;
	JButton ok;
	private MainActivity listener;

	public ModelPanel(final MainActivity listener) {
		setListener(listener);
		gridbag = new GridBagLayout();
		constraints = new GridBagConstraints();
		setLayout(gridbag);
		ok = new JButton("Хоккей");
		ok.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
			    System.out.println("OLOLOLOLOL this is model panel");
				
			    
			    listener.onSaveModel();
			}
		});

	}

	public void setItems(List<ModelItem> elements) {
		this.removeAll();
		this.elements = elements;
		for (ModelItem currentItem : elements) {
			constraints.insets = new Insets(10, currentItem.getLevel() * 80,
					10, 0);
			constraints.gridx = 0;
			gridbag.setConstraints(currentItem, constraints);
			add(currentItem);

		}
		constraints.insets = new Insets(10, 10, 10, 0);
		constraints.gridx = 0;
		gridbag.setConstraints(ok, constraints);
		add(ok);
		validate();
	}

	public void setData(XmlItem data) {
		setItems(elements);
	}

	@Override
	public void setListener(MainActivity listener) {
		this.listener = listener;
	}
	
	public List<ModelItem> getCheckedModelItems (){
	    List<ModelItem> checkedElements= new ArrayList<ModelItem>();
	    for (ModelItem item:elements){
	        if (item.isSelected()){
	            checkedElements.add(item);
	        }
	    }
	    return checkedElements;
	}

}
